package math_generic;

public class Vector2 
{
	public float x;
	public float y;
		
	public Vector2(float x, float y)
	{
		this.x = x;
		this.y = y;	
	}
	
	/* Calculates the magnitude of the vector */
	public float magnitude()
	{
		return (float) Math.sqrt(x*x + y*y);
	}
	
	/* Normalized the vector components */
	public Vector2 normalize()
	{
		float mag = magnitude();
		return (new Vector2(x/mag, y/mag)); 
	}
	
	public static float distanceFromOrigin(float x, float y)
	{
		return (float) Math.sqrt(x*x + y*y);
	}
	
	public static float distance(float x1, float y1, float x2, float y2)
	{
		return (float) Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
	}
	
	public void rotate(float angleDeg)
	{
		float cosAngle = (float) Math.cos(angleDeg*Math.PI/180);
		float sinAngle = (float) Math.sin(angleDeg*Math.PI/180);
		
		this.x = this.x*cosAngle - this.y*sinAngle;
		this.y = this.x*sinAngle + this.y*cosAngle;
	}
	
	public static Vector2 rotate(Vector2 vector, float angleDeg)
	{
		float cosAngle = (float) Math.cos(angleDeg*Math.PI/180);
		float sinAngle = (float) Math.sin(angleDeg*Math.PI/180);
		
		return new Vector2(vector.x*cosAngle - vector.y*sinAngle, 
						   vector.x*sinAngle + vector.y*cosAngle);
	}
}
